package bean.dao;

import hbt.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import bean.Proveedor;

public class ProveedorDAO{
	private static ProveedorDAO instancia = null;
	private static SessionFactory sf = null;

	public static ProveedorDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new ProveedorDAO();
		} 
		return instancia;
	}
	
	public void altaProveedor (String cuil, String razonSocial){
		Proveedor proveedor = new Proveedor ();
		proveedor.setCuil(cuil);
		proveedor.setRazonSocial(razonSocial);
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(proveedor);
		session.flush();
		session.getTransaction().commit();
		session.close();		
	}
	
	public Proveedor buscarProveedor (String cuil){
		Proveedor proveedor = new Proveedor();
		Session session = sf.openSession();
		Query query = session.createQuery("from Proveedor p where p.cuil=:cuil");
		query.setParameter("cuil", cuil);
		proveedor = (Proveedor) query.uniqueResult();
		session.close();
		return proveedor;
	}
	
}
